# -*- coding:utf-8 -*-
# https://www.cnblogs.com/cmmdc/p/7216726.html，根据这个解释可以知道，在该问题中，n阶的约瑟夫环问题是n-1阶约瑟夫环问题加上n阶约瑟夫环问题去除的第一个点+1
class Solution:
    def LastRemaining_Solution(self, n, m):
        # write code here
        if n < 1:
            return -1
        res = 0
        i = 2
        while i <= n:
            res = (res + m) % i
            i += 1
        return res



class Solution:
    def LastRemaining_Solution(self, n, m):
        if n < 1:
            return -1
        if n == 1:
            return 0
        return (self.LastRemaining_Solution(n-1,m)+m)%n# 用递归方法更好理解，n阶约瑟夫环问题，是n-1阶约瑟夫环问题加一次迭代的结果




test = Solution()
print(test.LastRemaining_Solution(5, 3))
